﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Admin.ViewModels;
using SDT.Lib.Helpers;
using SDT.Lib.Common;

namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Administrator)]
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        SDTDataContext eDataContext = new SDTDataContext();
        public ActionResult Index()
        {
            ReportViewModel r = new ReportViewModel();
            var fromDate = Request["fromDate"] != null && Request["fromDate"] != string.Empty ?
                Utils.ParseDateTime(Request["fromDate"], new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now.AddYears(-100);
            var toDate = Request["toDate"] != null && Request["toDate"] != string.Empty ?
                Utils.ParseDateTime(Request["toDate"], new System.Globalization.CultureInfo("vi-VN")) : DateTime.Now.AddYears(-100);

            r.TotalProducts = eDataContext.Products
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .ToList().Count;
            r.TotalProductsNotActive = eDataContext.Products
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.PubYN == false || e.PubYN == null).ToList().Count;
            r.TotalProductsVip1 = eDataContext.Products
                 .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.HotYN == true).ToList().Count;
            
            r.TotalQuestions = eDataContext.Questions
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .ToList().Count;
            r.TotalQuestionsNotAnswer = eDataContext.Questions
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.Answers.Count==0).ToList().Count;
            r.TotalQuestionsNotActive = eDataContext.Questions
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.PubYN==false).ToList().Count;
            r.TotalAnswerNotActive = eDataContext.Answers
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.PubYN == false).ToList().Count;

            r.TotalUsers = eDataContext.Customers
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .ToList().Count;
            r.TotalUsersLocked = eDataContext.Customers
                .Where(e => fromDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate >= fromDate : true)
                .Where(e => toDate.Year > DateTime.Now.AddYears(-100).Year ? e.AddDate <= toDate : true)
                .Where(e => e.IsLockedOut == true).ToList().Count;

            r.FromDate = fromDate.Year > DateTime.Now.AddYears(-100).Year ? fromDate : DateTime.MinValue;
            r.ToDate = toDate.Year > DateTime.Now.AddYears(-100).Year ? toDate : DateTime.MinValue;
            return View(r);
        }

    }
}
